package com.tgq.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgq.demo.common.Constants;
import com.tgq.demo.common.Result;
import com.tgq.demo.entity.Dict;
import com.tgq.demo.mapper.DictMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tgq.demo.service.IMenuService;
import com.tgq.demo.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陶国庆
 * @since 2023-02-10
 */
@RestController
@RequestMapping("/menu")
@Tag(name = "menu-controller",description = "菜单管理")
public class MenuController {

    @Resource
    private IMenuService menuService;   //导service包
    @Resource
    private DictMapper dictMapper;

    @PostMapping
    @ApiOperation("保存操作（更新或新增）")
    public Result save(@RequestBody Menu menu) {
        menuService.saveOrUpdate(menu);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除菜单")
    public Result delete(@PathVariable Integer id) {
        menuService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    @ApiOperation("批量删除菜单")
    public Result deleteBatch(@RequestBody List<Integer> ids){  //[1,2,3]
        menuService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    @ApiOperation("查询所有菜单")
    public Result findAll(@RequestParam(defaultValue = "") String name) {  //defaultValue设置默认值

        List<Menu> menuslist = menuService.findMenus(name);
        return Result.success(menuslist);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜单")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }

    @GetMapping("/page")
    @ApiOperation("分页")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);     //条件查询
        queryWrapper.orderByDesc("id");
        return  Result.success(menuService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @GetMapping("/icons")
    @ApiOperation("查询图标")
    public Result getIcons(){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictMapper.selectList(queryWrapper));
    }
}

